<template>
	<div class="diy_edit page_pet_foster_care" id="pet_foster_care_edit">
		<div class='warp'>
			<div class='container'>
				<div class='row diy_edit_content_box'>
					<div v-if="$check_field('set','pet_name') || $check_field('add','pet_name') || $check_field('get','pet_name')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>宠物名称:
							</span>
						</div>
						<!-- 文本 -->
						<div class="diy_field diy_text">
							<input type="text" id="form_pet_name" v-model="form['pet_name']" placeholder="请输入宠物名称" v-if="(form['pet_name'] && $check_field('set','pet_name')) || (!form['pet_name'] && $check_field('add','pet_name'))"  :disabled="disabledObj['pet_name_isDisabled']"/>
							<span v-else-if="$check_field('get','pet_name')">{{ form[''] }}</span>
						</div>
					</div>
					<div v-if="$check_field('set','pet_type') || $check_field('add','pet_type') || $check_field('get','pet_type')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>宠物类型:
							</span>
						</div>
						<!-- 选项 -->
						<div class="diy_field diy_down">
							<select id="form_pet_type" v-model="form['pet_type']" v-if="(form['pet_type'] && $check_field('set','pet_type')) || (!form['pet_type'] && $check_field('add','pet_type'))" >
								<option v-for="o in list_pet_type" :value="o['pet_type']">
									{{ o['pet_type'] }}
								</option>
							</select>
							<span v-else-if="$check_field('get','pet_type')">{{ form['pet_type'] }}</span>
						</div>
					</div>
					<div v-if="$check_field('set','pet_sex') || $check_field('add','pet_sex') || $check_field('get','pet_sex')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>宠物性别:
							</span>
						</div>
						<!-- 文本 -->
						<div class="diy_field diy_text">
							<input type="text" id="form_pet_sex" v-model="form['pet_sex']" placeholder="请输入宠物性别" v-if="(form['pet_sex'] && $check_field('set','pet_sex')) || (!form['pet_sex'] && $check_field('add','pet_sex'))"  :disabled="disabledObj['pet_sex_isDisabled']"/>
							<span v-else-if="$check_field('get','pet_sex')">{{ form[''] }}</span>
						</div>
					</div>
					<div v-if="$check_field('set','pet_age') || $check_field('add','pet_age') || $check_field('get','pet_age')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>宠物年龄:
							</span>
						</div>
						<!-- 文本 -->
						<div class="diy_field diy_text">
							<input type="text" id="form_pet_age" v-model="form['pet_age']" placeholder="请输入宠物年龄" v-if="(form['pet_age'] && $check_field('set','pet_age')) || (!form['pet_age'] && $check_field('add','pet_age'))"  :disabled="disabledObj['pet_age_isDisabled']"/>
							<span v-else-if="$check_field('get','pet_age')">{{ form[''] }}</span>
						</div>
					</div>
					<div v-if="$check_field('set','foster_care_status') || $check_field('add','foster_care_status') || $check_field('get','foster_care_status')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>寄养状态:
							</span>
						</div>
						<!-- 选项 -->
						<div class="diy_field diy_down">
							<select id="form_foster_care_status" v-model="form['foster_care_status']" v-if="(form['foster_care_status'] && $check_field('set','foster_care_status')) || (!form['foster_care_status'] && $check_field('add','foster_care_status'))" >
								<option v-for="o in list_foster_care_status" :value="o">
									{{o}}
								</option>
							</select>
							<span v-else-if="$check_field('get','foster_care_status')">{{ form['foster_care_status'] }}</span>
						</div>
					</div>
					<div v-if="$check_field('set','foster_care_pictures') || $check_field('add','foster_care_pictures') || $check_field('get','foster_care_pictures')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>寄养图片:
							</span>
						</div>
						<!-- 图片 -->
						<input type="file" :disabled="disabledObj['foster_care_pictures_isDisabled']" style="display: none;" id="form_img_foster_care_pictures" title="foster_care_pictures" @change="change_file($event.target.files,'foster_care_pictures')"/>
						<!-- 修改权限 -->
						<div class="diy_field diy_img" v-if="form['foster_care_pictures'] && $check_field('set','foster_care_pictures')">
							<label for="form_img_foster_care_pictures">
								<img :src="$fullUrl(form['foster_care_pictures'])" />
							</label>
						</div>
						<!-- 添加权限 -->
						<div class="diy_field diy_img" v-else-if="!form['foster_care_pictures'] && $check_field('add','foster_care_pictures')">
							<label for="form_img_foster_care_pictures">
								<div class="btn_add_img">
									<span>+</span>
								</div>
							</label>
						</div>
						<!-- 查询权限 -->
						<div class="diy_field diy_img" v-else-if="$check_field('get','foster_care_pictures')">
							<img :src="$fullUrl(form['foster_care_pictures'])" />
						</div>
					</div>
					<div v-if="$check_field('set','user_name') || $check_field('add','user_name') || $check_field('get','user_name')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>用户姓名:
							</span>
						</div>
						<div class="diy_field diy_down">
							<select id="form_user_name" :disabled="disabledObj['user_name_isDisabled']" v-model="form['user_name']" v-if="(form['user_name'] && $check_field('set','user_name')) || (!form['user_name'] && $check_field('add','user_name'))" >
								<option v-for="o in list_user_user_name" :value="o['user_id']">
									{{o['nickname'] + '-' + o['username']}}
								</option>
							</select>
							<span v-else-if="$check_field('get','user_name')">{{ form['user_name'] }}</span>
						</div>
					</div>
					<div v-if="$check_field('set','foster_care_time') || $check_field('add','foster_care_time') || $check_field('get','foster_care_time')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>寄养时间:
							</span>
						</div>
						<!-- 日长 -->
						<div class="diy_field diy_datetime">
							<input type="datetime-local" :disabled="disabledObj['foster_care_time_isDisabled']" id="form_foster_care_time" v-model="form['foster_care_time']" placeholder="请输入寄养时间" v-if="(form['foster_care_time'] && $check_field('set','foster_care_time')) || (!form['foster_care_time'] && $check_field('add','foster_care_time'))" />
							<span v-else-if="$check_field('get','foster_care_time')">{{ form[''] }}</span>
						</div>
					</div>
					<div v-if="$check_field('set','foster_care_remarks') || $check_field('add','foster_care_remarks') || $check_field('get','foster_care_remarks')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>寄养备注:
							</span>
						</div>
						<!-- 多文本 -->
						<div class="diy_field diy_desc">
							<textarea id="form_foster_care_remarks" v-model="form['foster_care_remarks']" v-if="(form['foster_care_remarks'] && $check_field('set','foster_care_remarks')) || (!form['foster_care_remarks'] && $check_field('add','foster_care_remarks'))" :disabled="disabledObj['foster_care_remarks_isDisabled']" />
							<span v-else-if="$check_field('get','foster_care_remarks')">{{ form[''] }}</span>
						</div>
					</div>
				</div>
				<div class="diy_edit_submit_box row">
					<div class="col-12">
						<div class="btn_box">
							<button class="btn_submit" @click="submit()">提交</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import mixin from "@/mixins/page.js";
	export default {
		mixins: [mixin],
		components: {},
		data() {
			return {
				url_get_obj: "~/api/pet_foster_care/get_obj?",
				url_add: "~/api/pet_foster_care/add?",
				url_set: "~/api/pet_foster_care/set?",

				// 登录权限
				oauth: {
					"signIn": true,
					"user_group": []
				},

				// 查询条件
				query: {
					"pet_name": "",
					"pet_type": "",
					"pet_sex": "",
					"pet_age": "",
					"foster_care_status": "",
					"foster_care_pictures": "",
					"user_name": 0,
					"foster_care_time": "",
					"foster_care_remarks": "",
					"pet_foster_care_id": 0,
				},

				obj: {
					"pet_name":'', // 宠物名称
					"pet_type":'', // 宠物类型
					"pet_sex":'', // 宠物性别
					"pet_age":'', // 宠物年龄
					"foster_care_status":'', // 寄养状态
					"foster_care_pictures":'', // 寄养图片
					"user_name": 0, // 用户姓名
					"foster_care_time": new Date().getTime(),
					"foster_care_remarks":'', // 寄养备注
					"pet_foster_care_id": 0,
				},

				// 表单字段
				form: {
					"pet_name":'', // 宠物名称
					"pet_type":'', // 宠物类型
					"pet_sex":'', // 宠物性别
					"pet_age":'', // 宠物年龄
					"foster_care_status":'', // 寄养状态
					"foster_care_pictures":'', // 寄养图片
					"user_name": 0, // 用户姓名
					"foster_care_time": new Date().getTime(),
					"foster_care_remarks":'', // 寄养备注
					"pet_foster_care_id": 0,
				},
				disabledObj:{
					"pet_name_isDisabled": false,
					"pet_type_isDisabled": false,
					"pet_sex_isDisabled": false,
					"pet_age_isDisabled": false,
					"foster_care_status_isDisabled": false,
					"foster_care_pictures_isDisabled": false,
					"user_name_isDisabled": false,
					"foster_care_time_isDisabled": false,
					"foster_care_remarks_isDisabled": false,
				},
				//宠物类型选项列表
				list_pet_type: [],
				//寄养状态选项列表
				list_foster_care_status: ['待寄养','已寄养'],
				// 用户列表
				list_user_user_name: [],

				// ID字段
				field: "pet_foster_care_id",
			}
		},
		methods: {
			/**
			 * 获取宠物类型列表
			 */
			async get_list_pet_type() {
				var json = await this.$get("~/api/pet_classification/get_list?");
				if(json.result && json.result.list){
					this.list_pet_type = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			/**
			 * 获取普通用户用户列表
			 */
			async get_list_user_user_name() {
				// if(this.user_group !== "管理员" && this.form["user_name"] === 0) {
				//     this.form["user_name"] = this.user.user_id;
				// }
				var json = await this.$get("~/api/user/get_list?user_group=普通用户");
				if(json.result && json.result.list){
					this.list_user_user_name = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			async get_user_session_user_name(){
				var _this = this;
				var json = await this.$get("~/api/user_group/get_obj?name=普通用户");
				if(json.result && json.result.obj){
					var source_table = json.result.obj.source_table;
					var user_id = _this.$store.state.user.user_id;
					if (user_id){
						var url = "~/api/"+source_table+"/get_obj?"
						this.$get(url, {"user_id":_this.$store.state.user.user_id}, function(res) {
							if (res.result && res.result.obj) {
								var arr = []
								for (let key in res.result.obj) {
									arr.push(key)
								}
								var arrForm = []
								for (let key in _this.form) {
									arrForm.push(key)
								}
								_this.form["user_name"] = user_id
								_this.disabledObj['user_name' + '_isDisabled'] = true
								for (var i=0;i<arr.length;i++){
                  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
                    for (var j = 0; j < arrForm.length; j++) {
                      if (arr[i] === arrForm[j]) {
                        if (arr[i] !== "user_name") {
                          _this.form[arrForm[j]] = res.result.obj[arr[i]]
                          _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                          break;
                        }
                      }
                    }
                  }
								}
							}
						});
					}
				}
				else if(json.error){
					console.error(json.error);
				}
			},

			/**
			 * 修改文件
			 * @param { Object } files 上传文件对象
			 * @param { String } str 表单的属性名
			 */
			change_file(files, str) {
				var form = new FormData();
				form.append("file", files[0]);
				this.$post("~/api/pet_foster_care/upload?", form, (res) => {
					if (res.result) {
						this.form[str] = res.result.url;
					} else if (res.error) {
						this.$toast(res.error.message);
					}
				});
			},

			/**
			 * 获取对象后获取缓存表单
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_before(param){
				var form = $.db.get("form");
				// if (form) {
        //   delete(form.examine_state)
        //   delete(form.examine_reply)
        //   this.obj = $.push(this.obj ,form);
				// 	this.form = $.push(this.form ,form);
				// }
				// var arr = []
				// for (let key in form) {
				// 	arr.push(key)
				// }
				// for (var i=0;i<arr.length;i++){
				// 	this.disabledObj[arr[i] + '_isDisabled'] = true
				// }
        if (form) {
          var arr = []
          for (let key in form) {
            arr.push(key)
          }
          var arrForm = []
          for (let key in this.form) {
            arrForm.push(key)
          }
          for (var i=0;i<arr.length;i++){
            if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
              for (var j = 0; j < arrForm.length; j++) {
                if (arrForm[j] === arr[i]) {
                  this.form[arrForm[j]] = form[arr[i]]
                  this.obj[arrForm[j]] = form[arr[i]]
                  this.disabledObj[arrForm[j] + '_isDisabled'] = true
                  break;
                }
              }
            }
          }
        }
        if (JSON.stringify(this.form["foster_care_time"]).indexOf("-")===-1) {
          this.form["foster_care_time"] = this.$toTime(parseInt(this.form["foster_care_time"]), "yyyy-MM-ddThh:mm")
        }

        $.db.del("form");
				return param;
			},

			/**
			 * 获取对象后获取缓存表单
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json ,func){
				// var form = $.db.get("form");
				// var obj = Object.assign({} ,form ,this.obj);
				// if (obj) {
        //   delete(obj.examine_state)
        //   delete(obj.examine_reply)
				// 	this.obj = $.push(this.obj ,obj);
				// }
				// if (form) {
        //   delete(form.examine_state)
        //   delete(form.examine_reply)
				// 	this.form = $.push(this.form ,form);
				// }
				if(func){
					func(json);
				}
			},

		},
		created() {
			this.get_list_pet_type();
			this.get_user_session_user_name();
			this.get_list_user_user_name();
		},
	}
</script>

<style>
</style>
